package com.bootdo.backlist.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.backlist.dto.MessageDTO;
import com.bootdo.common.interfaces.Add;
import com.bootdo.common.interfaces.Update;
import com.bootdo.backlist.converter.ConvertDomainUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.backlist.form.MessageForm;
import com.bootdo.backlist.vo.MessageVO;
import com.bootdo.backlist.service.MessageService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 发帖表
 * 
 * @author zengcx
 * @email 13851161069@163.com
 * @date 2019-04-08 20:16:42
 */
 
@Controller
@Slf4j
@RequestMapping("/backlist/message")
public class MessageController {
	@Autowired
	private MessageService messageService;
	
	@GetMapping()
	@RequiresPermissions("backlist:message:message")
	String Message(){
	    return "backlist/message/message";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("backlist:message:message")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MessageVO> messageList = ConvertDomainUtils.convertList(messageService.list(query),MessageVO.class);
		int total = messageService.count(query);
		PageUtils pageUtils = new PageUtils(messageList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("backlist:message:add")
	String add(){
	    return "backlist/message/add";
	}

	@GetMapping("/edit/{blackId}")
	@RequiresPermissions("backlist:message:edit")
	String edit(@PathVariable("blackId") Long blackId,Model model){
		MessageDTO message = messageService.get(blackId);
		model.addAttribute("message", message);
	    return "backlist/message/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("backlist:message:add")
	public R save(@Validated(Add.class) MessageForm messageForm, BindingResult bindingResult){
	    if (bindingResult.hasErrors()){
	        log.error("【传参错误】,messageForm={}",messageForm);
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
		return messageService.save(ConvertDomainUtils.convertObject(messageForm,MessageDTO.class)) ;
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("backlist:message:edit")
	public R update(@RequestBody @Validated(Update.class) MessageForm messageForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("【传参错误】,messageForm={}",messageForm);
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
		return  messageService.update(ConvertDomainUtils.convertObject(messageForm,MessageDTO.class)) ;
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("backlist:message:remove")
	public R remove( Integer blackId){
	    return messageService.remove(blackId);
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("backlist:message:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] blackIds){
		return messageService.batchRemove(blackIds);
	}
	
}
